import 'package:flutter_test/flutter_test.dart';
import '../lib/common/entity/share.dart';

void main() {
  group('Publish Logic Tests', () {
    test('should create ShareEntity with required fields', () {
      final share = ShareEntity(
        id: 'test_1',
        description: 'Test description',
        image: 'test.jpg',
        emotion: 'happy',
        date: '2025-08-06T10:00:00.000Z',
        commentIds: [],
        browses: 0,
        video: '',
      );

      expect(share.id, 'test_1');
      expect(share.description, 'Test description');
      expect(share.image, 'test.jpg');
      expect(share.emotion, 'happy');
      expect(share.hasImage, true);
      expect(share.hasVideo, false);
    });

    test('should create ShareEntity with video', () {
      final share = ShareEntity(
        id: 'test_2',
        description: 'Video test',
        image: '',
        emotion: 'love',
        date: '2025-08-06T10:00:00.000Z',
        commentIds: [],
        browses: 0,
        video: 'test_video.mp4',
      );

      expect(share.hasVideo, true);
      expect(share.hasImage, false);
      expect(share.video, 'test_video.mp4');
    });

    test('should validate emotion selection', () {
      final validEmotions = ['happy', 'angry', 'love', 'disappointed', 'bored'];
      
      for (final emotion in validEmotions) {
        final share = ShareEntity(
          id: 'test_emotion_$emotion',
          description: 'Test $emotion',
          image: 'test.jpg',
          emotion: emotion,
          date: '2025-08-06T10:00:00.000Z',
          commentIds: [],
          browses: 0,
          video: '',
        );
        
        expect(share.emotion, emotion);
      }
    });

    test('should handle media validation logic', () {
      // 测试只有图片的情况
      final imageOnlyShare = ShareEntity(
        id: 'image_only',
        description: 'Image only',
        image: 'test.jpg',
        emotion: 'happy',
        date: '2025-08-06T10:00:00.000Z',
        commentIds: [],
        browses: 0,
        video: '',
      );
      
      expect(imageOnlyShare.hasImage, true);
      expect(imageOnlyShare.hasVideo, false);
      expect(_hasMedia(imageOnlyShare), true);

      // 测试只有视频的情况
      final videoOnlyShare = ShareEntity(
        id: 'video_only',
        description: 'Video only',
        image: '',
        emotion: 'happy',
        date: '2025-08-06T10:00:00.000Z',
        commentIds: [],
        browses: 0,
        video: 'test.mp4',
      );
      
      expect(videoOnlyShare.hasImage, false);
      expect(videoOnlyShare.hasVideo, true);
      expect(_hasMedia(videoOnlyShare), true);

      // 测试没有媒体的情况
      final noMediaShare = ShareEntity(
        id: 'no_media',
        description: 'No media',
        image: '',
        emotion: 'happy',
        date: '2025-08-06T10:00:00.000Z',
        commentIds: [],
        browses: 0,
        video: '',
      );
      
      expect(noMediaShare.hasImage, false);
      expect(noMediaShare.hasVideo, false);
      expect(_hasMedia(noMediaShare), false);
    });

    test('should validate required content fields', () {
      // 模拟发布验证逻辑
      expect(_validatePublishData('happy', 'Test content', true), true);
      expect(_validatePublishData(null, 'Test content', true), false); // 没有情绪
      expect(_validatePublishData('happy', '', true), false); // 没有内容
      expect(_validatePublishData('happy', 'Test content', false), false); // 没有媒体
    });

    test('should generate unique share IDs', () {
      final id1 = DateTime.now().millisecondsSinceEpoch.toString();
      // 等待一毫秒确保ID不同
      Future.delayed(Duration(milliseconds: 1));
      final id2 = DateTime.now().millisecondsSinceEpoch.toString();
      
      expect(id1, isNotEmpty);
      expect(id2, isNotEmpty);
      // 注意：在快速执行的测试中，ID可能相同，这在实际应用中不太可能发生
    });
  });
}

// 辅助函数：检查是否有媒体
bool _hasMedia(ShareEntity share) {
  return share.hasImage || share.hasVideo;
}

// 辅助函数：验证发布数据
bool _validatePublishData(String? emotion, String content, bool hasMedia) {
  if (emotion == null || emotion.isEmpty) return false;
  if (content.trim().isEmpty) return false;
  if (!hasMedia) return false;
  return true;
}
